package com.woniuxy.yuekeuser.controller;


import com.woniuxy.yuekeuser.common.Result;
import com.woniuxy.yuekeuser.common.UtilMethod;
import com.woniuxy.yuekeuser.controller.Form.HotelCarMassage;
import com.woniuxy.yuekeuser.info.UserInfo;
import com.woniuxy.yuekeuser.model.TOrder;
import com.woniuxy.yuekeuser.model.User;
import com.woniuxy.yuekeuser.service.IOrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Freedom
 * @since 2020-11-03
 */
@Slf4j
@CrossOrigin("*")
@RestController
public class OrderController {
    static TOrder tOrder;
    @Autowired
    private IOrderService orderService;

    @PostMapping("/order/placeOrder")
    public Result placeOrder(HttpSession session,  @RequestBody HotelCarMassage hotelCarMassage){
        log.info("订单信息为{}",hotelCarMassage);
        //获取用户的信息
        UserInfo user=null;
        Object obj=session.getAttribute("User");
        if(obj instanceof UserInfo){
            user=(UserInfo) obj;
        }else {
            return Result.fail("亲，请先登录哦");
        }

//        order.setUserId(user.getUserId());
        TOrder order=new TOrder();
        order.setOrderPrice(hotelCarMassage.getOrderprice());
        order.setOrderType(hotelCarMassage.getHotolcarName());
        order.setUserId(user.getUserId());



        order.setOrderTime(UtilMethod.getTime(hotelCarMassage.getOrdertime()[0]+" 00:00:00"));
        order.setOrderEndtime(UtilMethod.getTime(hotelCarMassage.getOrdertime()[1]+" 00:00:00"));
        order.setOrderState("未支付");
        order.setDownorderTime(LocalDateTime.now());
//        if(order==null){
//            return Result.fail("订单异常");
//        }
        log.info("加入表中的数据为：{}",order);
       int id= orderService.placeOrder(order);
//       将数据存入sesion
        order.setOrderId(id);
        session.setAttribute("Order",order);
        tOrder=order;
        return Result.success();

    }
    //修改订单状态
    @PostMapping("/changeOrderState")
    public void changeOrderState(HttpSession session){
//        Object obj=session.getAttribute("Order");
//        TOrder order=null;
//        if(obj instanceof TOrder){
//            order=(TOrder)obj;
//        }

//        修改订单状态
        log.info("订单成功后获取到的数据{}",tOrder);
        tOrder.setOrderState("完成");
        log.info("修改的数据为{}",tOrder);
        orderService.changeOrderState(tOrder);

    }

}

